#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int SIZE = 64;
LL lineBase[SIZE];
LL mark[SIZE];
int m_cnt;
inline void Init() {
    memset(lineBase, 0, sizeof(lineBase));
}
bool Insert(LL t) {
    for (int i=SIZE-1; i>=0; --i) {
        LL w = (1ll<<i);
        if (!(t&w)) continue;
        if (lineBase[i]) t ^= lineBase[i];
        else {
            for (int j=0; j<i; ++j) if (t&(1ll<<j)) t ^= lineBase[j];
            lineBase[i] = t;
            for (int j=i+1; j<SIZE; ++j) if (lineBase[j]&w) lineBase[j] ^= t;
            return true;
        }
    }
    return false;
}
inline void setMark() {
    m_cnt = 0;
    for (int i=0; i<SIZE; ++i) {
        if (lineBase[i]) mark[m_cnt++] = lineBase[i];
    }
}
void show() {
    for (int i=0; i<SIZE; ++i) {
        for (int j=SIZE-1; j>=0; --j) printf("%d ", (bool)(lineBase[i]&(1ll<<j)));
        printf("\n");
    }
}

int main() {
#ifdef hai
    freopen("in.txt", "r", stdin);
#endif
    int N,Q;
    int T;
    cin>>T;
    for (int cas = 1; cas <= T; ++cas) {
        Init();
        scanf("%d", &N);
        LL t;
        bool flag = false;
        for (int i=0; i<N; ++i) {
            scanf("%lld", &t);
            if (!Insert(t)) flag = true;
        }
        //show();
        setMark();
        //for (int i=0; i<m_cnt; ++i) printf("%lld\n", mark[i]);
        //printf("flag: %d\n", flag);
        LL Max = (1ll<<m_cnt) - 1;
        if (flag) ++ Max;
        printf("Case #%d:\n", cas);
        scanf("%d", &Q);
        while(Q--) {
            scanf("%lld", &t);
            if (t>Max) printf("-1\n");
            else {
                if (t==1 && flag) {
                    printf("0\n");
                    continue;
                }
                if (flag) --t;
                LL res = 0;
                for (int i=0; i<m_cnt && t; ++i) {
                    if (t&1ll) {
                        res ^= mark[i];
                    }
                    t = (t >> 1);
                }
                printf("%lld\n", res);
            }
        }
    }
    return 0;
}
